package com.czwj.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.DataGridData;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.Project;
import com.czwj.entities.TreeNode;
import com.czwj.service.ProjectService;
import com.czwj.service.TreeNodeService;
import com.czwj.util.TextUtil;

@Controller
public class ProjectAction {

	@Resource
	TreeNodeService treeNodeService;
	
	@Resource
	ProjectService projectService;

	@RequestMapping(value = "projects", method = RequestMethod.GET)
	@ResponseBody
	public List<TreeNode> getProjectTree(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "id", required = false) String id){
		if(id!=null){
			return null;
		}
		List<TreeNode> nodes = new ArrayList<>();
		nodes = treeNodeService.findProject();
		for (TreeNode treeNode : nodes) {
			if(treeNode.getChildren().size()>0){
				treeNode.setState(TreeNode.STATE_OPEN);
			}else{
				treeNode.setState(TreeNode.STATE_CLOSED);
			}
		}
		return nodes;
	}
	
	@RequestMapping(value = "project", method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Project> getProjects(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows,@RequestParam(value = "supperid", required = false) int supperid) {
		if(supperid<0){
			return null;
		}
		DataGridData<Project> result = new DataGridData<>();
		Map<String, Object> params = new HashMap<>();
		PageBean pageBean = null;
		if (TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows))
			pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		if (pageBean != null) {
			params.put("start", pageBean.getStart());
			params.put("size", pageBean.getPageSize());
		}
		
		params.put("supperid", supperid);

		List<Project> projects = projectService.search(params);
		int total = projectService.getTotal(params);
		result.setRows(projects);
		result.setTotal(total);
		return result;
	}
	
	@SystemControllerLog(description = "修改菜单")
	@RequestMapping(value = "project", method = RequestMethod.POST)
	@ResponseBody
	public OperateResult add(@RequestBody Project project) {
		OperateResult result = new OperateResult();
		int count = 0;
		if (project.getId() !=null && project.getId()>0)
			count = projectService.update(project);
		else {
			project.setSort(projectService.getMaxSort(project.getSupperid())+1);
			count = projectService.insert(project);
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "删除菜单")
	@RequestMapping(value = "project", method = RequestMethod.DELETE)
	@ResponseBody
	public OperateResult delete(@RequestParam("projectid") int id) {
		OperateResult result = new OperateResult();
		int i = 0;
		i+=projectService.delete(id);
		i+=projectService.deleteBySupper(id);
		result.setSuccess(i > 0);
		return result;
	}
	
	@RequestMapping(value = "sort/project/{id}/{type}", method = RequestMethod.PUT)
	@ResponseBody
	public OperateResult resort(@PathVariable(value = "id") int id, @PathVariable(value = "type") int type) {
		OperateResult result = new OperateResult();
		Project  currentProject = projectService.selectByPrimaryKey(id);
		if (currentProject == null) {
			result.setErrMsg("排序失败");
			result.setSuccess(false);
			return result;
		}
		// 大于0 向下移动，小于0向上
		int currentSort = currentProject.getSort();
		int supperid = currentProject.getSupperid();
		if (type > 0) {
			int maxSort = projectService.getMaxSort(supperid);
			for (int i = currentSort + 1; i <= maxSort; i++) {
				Project nextProject = projectService.findBySort(supperid,i);
				if (nextProject != null)
					return exchange(currentProject, nextProject);
			}
			result.setErrMsg("已经是最后一个");
			result.setSuccess(false);
		} else {
			for (int i = currentSort - 1; i >= 0; i--) {
				Project prevProject = projectService.findBySort(supperid,i);
				if (prevProject != null) {
					return exchange(currentProject, prevProject);
				}
			}
			result.setErrMsg("已经是第一个");
			result.setSuccess(false);
		}
		return result;
	}

	private OperateResult exchange(Project pro1, Project pro2) {
		OperateResult result = new OperateResult();
		int sort1 = pro1.getSort();
		int sort2 = pro2.getSort();
		int tmp = 0;
		tmp = sort1;
		sort1 = sort2;
		sort2 = tmp;
		pro1.setSort(sort1);
		pro2.setSort(sort2);
		int count = 0;
		count += projectService.update(pro1);
		count += projectService.update(pro2);
		result.setSuccess(count == 2);
		return result;
	}
}
